package dimension.starry.block.dungeon;

import dimension.starry.constants.ItemQuality;
import dimension.starry.constants.PlayerDataType;
import dimension.starry.dataManager.PlayerDataManager;
import dimension.starry.item.typeInterface.ALLItemType;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.MapColor;
import net.minecraft.block.enums.Instrument;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static dimension.starry.constants.PlayerDataType.MAGIC;
import static dimension.starry.constants.PlayerDataType.MAX_MAGIC;

public class MagicJar extends Block implements ALLItemType {

    private static final Text MAGIC_FULL_TEXT = Text.translatable("starry_dimension.gui.message.magic_is_full");

    public MagicJar() {
        super(Settings.create()
            .mapColor(MapColor.OAK_TAN)
            .instrument(Instrument.BASS)
            .breakInstantly()
            .luminance(state -> 15)
            .nonOpaque()
            .sounds(BlockSoundGroup.GLASS));
    }

    @Override
    public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
        if (!world.isClient() && player instanceof ServerPlayerEntity serverPlayer) {
            int magic = PlayerDataManager.getInt(player, MAGIC);
            int maxMagic = PlayerDataManager.getInt(player, MAX_MAGIC);
            if (magic == maxMagic) {
                player.sendMessage(MAGIC_FULL_TEXT);
            } else {
                PlayerDataManager.setServerPlayerData(serverPlayer, PlayerDataType.MAGIC, maxMagic);
            }
        }
        super.onBreak(world, pos, state, player);
    }

    @Override
    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
        if (world.isClient()) {
            return ActionResult.PASS;
        }
        this.onBreak(world, pos, state, player);
        world.breakBlock(pos, false, player);
        return ActionResult.CONSUME;
    }

    @Override
    public ItemQuality getQuality() {
        return ItemQuality.EXCELLENT;
    }
}
